import { join } from 'path'
import { is } from '@electron-toolkit/utils'
import { shell, BrowserWindow } from 'electron'
import icon from '../../resources/icon.png?asset'

export function createVideoWindow(data: { id: string }) {
  // Create the browser window.
  const videoWindow = new BrowserWindow({
    x: 200,
    y: 200,
    width: 600,
    height: 600,
    show: false,
    autoHideMenuBar: true,
    frame: false,
    alwaysOnTop: true,
    skipTaskbar: true,
    resizable: false,
    minimizable: false,
    maximizable: false,
    transparent: true,
    ...(process.platform === 'linux' ? { icon } : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false,
    },
  })

  videoWindow.on('ready-to-show', () => {
    videoWindow.show()
  })

  videoWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)

    return { action: 'deny' }
  })

  // HMR for renderer base on electron-vite cli.
  // Load the remote URL for development or the local html file for production.
  if (is.dev && process.env.ELECTRON_RENDERER_URL) {
    videoWindow.loadURL(`${process.env.ELECTRON_RENDERER_URL}#/video?id=${data.id}`)
  } else {
    videoWindow.loadFile(join(__dirname, `../renderer/index.html`), {
      hash: `/video?id=${data.id}`,
    })
  }

  return videoWindow
}
